# import os
# import pandas as pd
# from data_processing import load_data, preprocess_data, create_spatial_features, save_processed_data
# from feature_engineering import (create_time_features, create_location_features,
#                                create_crime_pattern_features, create_temporal_patterns,
#                                create_interaction_features, prepare_features_for_model)
# from model import CrimePredictionModel, train_model
# from visualization import create_visualization_report, visualize_hotspots
# import warnings
# warnings.filterwarnings('ignore')
#
# def main():
#     """
#     主程序：运行完整的犯罪预测工作流
#     """
#     print("=== 城市犯罪热点预测系统 ===")
#
#     # 1. 创建必要的目录
#     print("\n1. 创建目录结构...")
#     for dir_path in ['data/raw', 'data/processed', 'data/models', 'data/visualizations']:
#         os.makedirs(dir_path, exist_ok=True)
#
#     # 2. 数据预处理
#     print("\n2. 数据预处理...")
#     try:
#         # 加载原始数据
#         df = load_data("data/raw/crime-data-los-angeles.csv")
#
#         # 预处理数据
#         df = preprocess_data(df)
#
#         # 创建空间特征
#         df = create_spatial_features(df)
#
#         # 保存预处理后的数据
#         save_processed_data(df, "data/processed/processed_crime_data.csv")
#         print("数据预处理完成！")
#
#     except FileNotFoundError:
#         print("错误：请确保原始数据文件 'crime-data-los-angeles.csv' 已放置在 data/raw 目录下")
#         return
#
#     # 3. 特征工程
#     print("\n3. 特征工程...")
#     df = pd.read_csv("data/processed/processed_crime_data.csv")
#     df['DATE OCC'] = pd.to_datetime(df['DATE OCC'])
#
#     # 应用特征工程
#     df = create_time_features(df)
#     df = create_location_features(df)
#     df = create_crime_pattern_features(df)
#     df = create_temporal_patterns(df)
#     df = create_interaction_features(df)
#
#     # 准备模型特征
#     final_features = prepare_features_for_model(df)
#     final_features.to_csv("data/processed/featured_crime_data.csv", index=False)
#     print("特征工程完成！")
#
#     # 4. 模型训练
#     print("\n4. 模型训练...")
#     model, results = train_model()
#     print("模型训练完成！")
#
#     # 5. 创建可视化
#     print("\n5. 创建可视化...")
#     create_visualization_report(df, results)
#
#     # 6. 预测热点
#     print("\n6. 预测热点...")
#     # 使用训练好的模型预测热点
#     hotspots, probabilities = model.predict_hotspots(final_features)
#
#     # 可视化热点预测结果
#     hotspot_map = visualize_hotspots(df, hotspots, probabilities,
#                                    save_path="data/visualizations/predicted_hotspots.html")
#
#     print("\n=== 处理完成！===")
#     print("""
# 结果文件位置：
# - 预处理后的数据：data/processed/processed_crime_data.csv
# - 特征工程后的数据：data/processed/featured_crime_data.csv
# - 训练好的模型：data/models/best_crime_prediction_model.joblib
# - 可视化结果：data/visualizations/
#     - crime_heatmap.html：犯罪热力图
#     - crime_time_distribution.png：时间分布图
#     - crime_types_distribution.png：犯罪类型分布
#     - area_crime_density.png：区域犯罪密度
#     - model_comparison.png：模型性能比较
#     - predicted_hotspots.html：预测的犯罪热点
#     """)
#
# if __name__ == "__main__":
#     main()


import os
import pandas as pd
from data_processing import load_data, preprocess_data, create_spatial_features, save_processed_data
from feature_engineering import (create_time_features, create_location_features,
                                 create_crime_pattern_features, create_temporal_patterns,
                                 create_interaction_features, prepare_features_for_model)
from model import CrimePredictionModel, train_model
from visualization import create_visualization_report, visualize_hotspots
import warnings

warnings.filterwarnings('ignore')

# 获取项目根目录
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def main():
    print("=== 城市犯罪热点预测系统 ===")

    # 1. 创建必要的目录
    print("\n1. 创建目录结构...")
    for sub_dir in ['data/raw', 'data/processed', 'data/models', 'data/visualizations']:
        os.makedirs(os.path.join(BASE_DIR, sub_dir), exist_ok=True)

    # 2. 数据预处理
    print("\n2. 数据预处理...")
    try:
        raw_data_path = os.path.join(BASE_DIR, "data", "raw", "crime-data-los-angeles.csv")
        df = load_data(raw_data_path)

        df = preprocess_data(df)
        df = create_spatial_features(df)

        processed_path = os.path.join(BASE_DIR, "data", "processed", "processed_crime_data.csv")
        save_processed_data(df, processed_path)
        print("数据预处理完成！")

    except FileNotFoundError:
        print("错误：请确保原始数据文件 'crime-data-los-angeles.csv' 已放置在 data/raw 目录下")
        return

    # 3. 特征工程
    print("\n3. 特征工程...")
    df = pd.read_csv(processed_path)
    df['DATE OCC'] = pd.to_datetime(df['DATE OCC'])

    df = create_time_features(df)
    df = create_location_features(df)
    df = create_crime_pattern_features(df)
    df = create_temporal_patterns(df)
    df = create_interaction_features(df)

    featured_path = os.path.join(BASE_DIR, "data", "processed", "featured_crime_data.csv")
    final_features = prepare_features_for_model(df)
    final_features.to_csv(featured_path, index=False)
    print("特征工程完成！")

    # 4. 模型训练
    print("\n4. 模型训练...")
    model_save_path = os.path.join(BASE_DIR, "data", "models", "best_crime_prediction_model.pkl")
    model, results = train_model(featured_path, processed_path, model_save_path)
    print("模型训练完成！")

    # 5. 创建可视化
    print("\n5. 创建可视化...")
    create_visualization_report(df, results)

    # 6. 预测热点
    print("\n6. 预测热点...")
    hotspots, probabilities = model.predict_hotspots(final_features)
    hotspot_map_path = os.path.join(BASE_DIR, "data", "visualizations", "predicted_hotspots.html")
    hotspot_map = visualize_hotspots(df, hotspots, probabilities, save_path=hotspot_map_path)

    print("\n=== 处理完成！===")
    print(f"""
结果文件位置：
- 预处理后的数据：{processed_path}
- 特征工程后的数据：{featured_path}
- 训练好的模型：data/models/best_crime_prediction_model.joblib
- 可视化结果：data/visualizations/
    - crime_heatmap.html：犯罪热力图
    - crime_time_distribution.png：时间分布图
    - crime_types_distribution.png：犯罪类型分布
    - area_crime_density.png：区域犯罪密度
    - model_comparison.png：模型性能比较
    - predicted_hotspots.html：预测的犯罪热点
    """)


if __name__ == "__main__":
    main()
